#!/bin/bash
set -euo pipefail
bundle="$(cd "$(dirname "$0")/../Resources" && pwd)"
root="$HOME/Library/Application Support/KMD Motor"
if [ "$(/usr/sbin/sysctl -n hw.optional.arm64)" != "1" ] || [ "$(/usr/bin/sw_vers -productVersion | /usr/bin/cut -d. -f1)" -lt 14 ]; then
 /usr/bin/osascript -e 'display dialog "Bu önizleme Apple Silicon ve macOS 14 veya üzerini gerektirir." buttons {"Tamam"}'
 exit 1
fi
/bin/mkdir -p "$root/engine-data"
exec >>"$root/engine-data/bootstrap.log" 2>&1
unset PYTHONHOME PYTHONPATH PIP_INDEX_URL PIP_EXTRA_INDEX_URL PIP_TRUSTED_HOST
export PIP_CONFIG_FILE=/dev/null PIP_NO_INPUT=1
if [ ! -f "$root/runtime/bin/python3" ]; then
 /bin/mkdir -p "$root/runtime"
 echo "ba32036ad8f4c434d97a1ee7feef8b7edc0fac16777639b97e39c74a108e4440  $bundle/python.tar.gz" | /usr/bin/shasum -a 256 -c -
 /usr/bin/tar -xzf "$bundle/python.tar.gz" --strip-components 1 -C "$root/runtime"
fi
# Package code is versioned; user models/projects live outside this release.
/bin/mkdir -p "$root/studio" "$root/agent" "$root/payloads" "$root/bin" "$root/araclar"
/usr/bin/ditto "$bundle/studio" "$root/studio"
/usr/bin/ditto "$bundle/agent" "$root/agent"
/usr/bin/ditto "$bundle/payloads" "$root/payloads"
/usr/bin/ditto "$bundle/araclar" "$root/araclar"
if [ ! -f "$root/engine-data/core-20261020.ok" ]; then
 "$root/runtime/bin/python3" -I -m pip install --only-binary=:all: --index-url https://pypi.org/simple --disable-pip-version-check --no-input -r "$bundle/core-requirements.txt"
 "$root/runtime/bin/python3" -I -c 'import aiohttp,psutil,cryptography,Foundation,cv2,onnxruntime,PIL'
 /usr/bin/touch "$root/engine-data/core-20261020.ok"
fi
exec "$root/runtime/bin/python3" -E -s "$root/agent/launch.py"
